package com.geen.tuoke.ui.dialog

import android.content.Context
import android.view.View
import androidx.databinding.DataBindingUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.geen.tuoke.R
import com.geen.tuoke.bean.PayMoneyInfo
import com.geen.tuoke.bean.RechargeInfo
import com.geen.tuoke.databinding.DialogRechargeBinding
import com.geen.tuoke.databinding.ItemRechargeBinding
import com.geen.tuoke.utils.ext.dp2px
import com.geen.tuoke.utils.ext.toastShort
import com.geen.tuoke.widget.dialog.DialogViewHolder
import com.geen.tuoke.widget.dialog.XXDialog
import com.kbeanie.multipicker.utils.LogUtils

/**
 * @Author LuoJi
 * @Date 2021/12/30-14:28
 * @Desc 话费充值
 */
class DialogRecharge(context: Context,private val payMoneyList:MutableList<PayMoneyInfo>,private val rechargeCallBack:(rechargeInfo:PayMoneyInfo)->Unit) : XXDialog(context, R.layout.dialog_recharge) {
    private var mBinding : DialogRechargeBinding? = null
    private var selectRechargeInfo:PayMoneyInfo? = null
    init {
        setWidthAndHeight(dp2px(288f), dp2px(374f))
        setCanceledOnTouchOutside(false)

        val mAdapter = object :BaseQuickAdapter<PayMoneyInfo,BaseDataBindingHolder<ItemRechargeBinding>>(R.layout.item_recharge){

            override fun convert(holder: BaseDataBindingHolder<ItemRechargeBinding>, item: PayMoneyInfo) {
                with(holder.dataBinding!!){
                    recharge = item
                    tvNumber.isSelected = item.isSelect
                    executePendingBindings()
                }
            }
        }

        mBinding?.mRecycleView?.adapter = mAdapter

        mAdapter.data = payMoneyList

        mAdapter.setOnItemClickListener { adapter, view, position ->
            val recharge = mAdapter.getItem(position)
            mAdapter.data.forEach {
                it.isSelect = false
            }
            recharge.isSelect = true
            selectRechargeInfo = recharge
            mAdapter.notifyDataSetChanged()
        }
    }

    override fun convert(holder: DialogViewHolder) {
        mBinding = DataBindingUtil.bind(holder.convertView)

        mBinding?.apply {

            viewClose.setOnClickListener {
                dismiss()
            }

            btnSure.setOnClickListener {
                if(selectRechargeInfo==null){
                    return@setOnClickListener
                }
                dismiss()
                rechargeCallBack(selectRechargeInfo!!)
            }
        }
    }
}